﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ZoLone.Logics.Sys;
using ZoLone.Models.Sys;
using Zo.Http;

namespace ZoLone.Areas.Admin.Controllers.Sys
{
    public class OperationController : BaseController
    {
        // GET: Admin/Operation
        OperationLogic _Logic = new OperationLogic();
        public ActionResult Index()
        {
            return View();
        }

        //[HttpPost]
        //public IActionResult GetData(TableBsEntity<Sys_Operation> tb)
        //{
        //    int pageIndex = tb.start / tb.length+1;
        //    int total = 0;
        //    var list = _Logic.GetOperationListByPage(pageIndex, tb.length, out total).ToList();
        //    tb.recordsTotal = total;
        //    tb.recordsFiltered = total;
        //    tb.data = list;
        //    return Success(tb);
        //}

        /// <summary>
        /// 获取所有菜单权限数据
        /// </summary>
        /// <returns></returns>
        public IActionResult GetData()
        {
            var tree = _Logic.GetOperationTree();
            return Success(tree);
        }

        /// <summary>
        /// 获取所有后台控制方法
        /// </summary>
        /// <returns></returns>
        public IActionResult GetALLActions()
        {
            var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes().Where(t => t.BaseType == typeof(BaseController)));
            var controllers = from type in types
                              where typeof(Controller).IsAssignableFrom(type) && !type.IsAbstract
                              orderby type.FullName
                              from m in type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
                              where !m.IsSpecialName
                              select "AdminAreas@" + type.FullName.Substring(type.FullName.LastIndexOf('.') + 1) + "@" + m.Name;
            var data = controllers.Select((p, i) => new { id = p, text = p });
            return Success(data);
        }

        /// <summary>
        /// 获取单条权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult GetRightById(int id)
        {
            Sys_Operation op = _Logic.GetOperaionById(id);
            return Success(op);
        }
        /// <summary>
        /// 保存权限菜单
        /// </summary>
        /// <returns></returns>
        public IActionResult Save()
        {
          
            int id = Convert.ToInt32(Request.Form["id"]);
            int parentId = Convert.ToInt32(Request.Form["ParentId"]);
            int isTopClick = Convert.ToInt32(Request.Form["isTopClick"]);
            string right = !String.IsNullOrEmpty(Request.Form["right"]) ? Request.Form["right"].ToString().Trim() : string.Empty;
            string name = !String.IsNullOrEmpty(Request.Form["name"]) ? Request.Form["name"].ToString().Trim() : string.Empty;
            string icon = !String.IsNullOrEmpty(Request.Form["icon"]) ? Request.Form["icon"].ToString().Trim() : string.Empty;
            if (string.IsNullOrEmpty(name))
            {
                return this.Error("Name required!");
            }
            if (!string.IsNullOrEmpty(right))
            {
                right = right.TrimEnd(',');
                right = right.UrlDecode();
                if (!right.Contains("@"))
                {
                    right = string.Empty;
                }
            }
            if (string.IsNullOrEmpty(icon))
            {
                icon = "fa fa-circle-o";
            }
            var result_id = _Logic.SaveRight(new Sys_Operation { Id = id, Name = name, Right = right, IsTopClickMenu = isTopClick, ParentId = parentId, Icon = icon });
            if (result_id > 0)
            {
                return this.Success(result_id);
            }
            return this.Error();
        }

        /// <summary>
        /// 更新权限菜单
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Menu()
        {
            string html = await this.RenderViewAsync("Menu", true);
            return Success(html);
        }
    }
}